<?php
function getHTMLVal($param,$default="") {
	$getValue="";
	$postValue="";
	if (isset($_GET[$param])) {
		$getValue=$_GET[$param];
	}
	if (isset($_POST[$param])) {
		$postValue=$_POST[$param]; 
	}

	$finalValue=$default;
	if (strlen($getValue)>0) {
		$finalValue=trim($getValue);
	} else {
		if (strlen($postValue)>0) {
			$finalValue=trim($postValue);
		} else {
	 		$finalValue=trim($default);
		}
	}

	return $finalValue;

}

function mysql_query_show_error($QUERY) {
  $RESULT = mysql_query($QUERY)
  or die("The following query failed:<br>\n".
         "$QUERY<br>\n".
         "MySQL error message: <br>\n".
         mysql_errno().": ".mysql_error()."<br>\n");

 return $RESULT;
}

function getParticipants($manifId, $includeBracket=true) {		
	if (strlen($manifId)>0) {
		$query="select id,longitude,latitude,title,personaltext,ref_id from PARTICIPANT where 	manifestation_id=".$manifId;
		
		$result_part=mysql_query_show_error($query);
	
		if ($includeBracket) {
			$res="{";
		}
		else {
			$res="";
		}
		$res.="\n\"points\": [\n";
		$points="";
	
		while ($row= mysql_fetch_object($result_part)) {
			if (strlen($points)>0){
				$points.=",";
			}
			$points.="\n{\"longitude\": \"".$row->longitude."\",";
			$points.="\n\"latitude\": \"".$row->latitude."\",";
			$points.="\n\"title\": \"".$row->title."\",";
			$points.="\n\"personaltext\": \"".$row->personaltext."\",";	
			$points.="\n\"id\": \"".$row->id."\"";
			
			$points.="\n}";
		}
		$res.=$points;
		$res.="\n]";

		if ($includeBracket) {
			$res.="}";
		}
		else {
			$res.="";
		}	
	}
return $res;
}

function getDemonstration($manifId, $withUsers=false) {
	if (strlen($manifId)>0) {
		$query="select id,longitude,latitude,title,shortname,description from manifestation where id=".$manifId;
		
	} else {
		$query="select id,longitude,latitude,title,shortname,description from manifestation";
	}
	$result=mysql_query_show_error($query);

	$res="{";
	
	$res.="\n\"demonstration\": [";
	$list="";

	while ($row= mysql_fetch_object($result)) {
		if (strlen($list)>0){
			$list.=",";
		}
		$list.="\n{\"longitude\": \"".$row->longitude."\",";
		$list.="\"latitude\": \"".$row->latitude."\",";
		$list.="\"title\": \"".$row->title."\",";
		$list.="\"shortname\": \"".$row->shortname."\",";	
		$list.="\"description\": \"".$row->description."\",";	
		$list.="\"id\": \"".$row->id."\"";
		
		$list.="}";
	}
	$res.=$list;
	$res.="]\n";

	if ($withUsers) {
		$res.= ",".getParticipants($manifId, false);
	}
	$res.="}";
	return $res;
}

?>
